package cn.tedu.test.rpc.demo05;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.Socket;

public class Stub {
    public static Object getStub(Class clazz) {
        InvocationHandler handler=new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Socket socket=new Socket("127.0.0.1",8000);

                ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
                String clazzName= clazz.getName();
                String methodName=method.getName();
                Class[] parametersTypes=method.getParameterTypes();
                oos.writeUTF(clazzName);
                oos.writeUTF(methodName);
                oos.writeObject(parametersTypes);
                oos.writeObject(args);
                oos.flush();

                ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
                Object o=ois.readObject();
                oos.close();
                socket.close();
                return o;
            }
        };
        Object o= Proxy.newProxyInstance(clazz.getClassLoader(),new Class[]{clazz},handler);
        System.out.println(o.getClass().getName());
        System.out.println(o.getClass().getInterfaces()[0]);
        return o;
    }
}
